home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / dejagnu.lha / dejagnu-1.0.1 / tcl / doc / TraceVar.3 < prev    next >
Text File  |  1993-02-14  |  14KB  |  339 lines

  1. '\"
  2. '\" Copyright 1989 Regents of the University of California
  3. '\" Permission to use, copy, modify, and distribute this
  4. '\" documentation for any purpose and without fee is hereby
  5. '\" granted, provided that this notice appears in all copies.
  6. '\" The University of California makes no representations about
  7. '\" the suitability of this material for any purpose.  It is
  8. '\" provided "as is" without express or implied warranty.
  9. '\" 
  10. .so man.macros
  11. .HS Tcl_TraceVar tcl
  12. .VS
  13. .BS
  14. .SH NAME
  15. Tcl_TraceVar, Tcl_TraceVar2, Tcl_UntraceVar, Tcl_UntraceVar2, Tcl_VarTraceInfo, Tcl_VarTraceInfo2 \- monitor accesses to a variable
  16. .SH SYNOPSIS
  17. .nf
  18. \fB#include <tcl.h>\fR
  19. .sp
  20. int
  21. \fBTcl_TraceVar(\fIinterp, varName, flags, proc, clientData\fB)\fR
  22. .sp
  23. int
  24. \fBTcl_TraceVar2(\fIinterp, name1, name2, flags, proc, clientData\fB)\fR
  25. .sp
  26. \fBTcl_UnTraceVar(\fIinterp, varName, flags, proc, clientData\fB)\fR
  27. .sp
  28. \fBTcl_UnTraceVar2(\fIinterp, name1, name2, flags, proc, clientData\fB)\fR
  29. .sp
  30. ClientData
  31. \fBTcl_VarTraceInfo(\fIinterp, varName, flags, proc, prevClientData\fB)\fR
  32. .sp
  33. ClientData
  34. \fBTcl_VarTraceInfo2(\fIinterp, name1, name2, flags, proc, prevClientData\fB)\fR
  35. .SH ARGUMENTS
  36. .AS Tcl_VarTraceProc prevClientData
  37. .AP Tcl_Interp *interp in
  38. Interpreter containing variable.
  39. .AP char *varName in
  40. Name of variable.  May refer to a scalar variable, to
  41. an array variable with no index, or to an array variable
  42. with a parenthesized index.
  43. .AP int flags in
  44. OR-ed combination of the values TCL_TRACE_READS, TCL_TRACE_WRITES, and
  45. TCL_TRACE_UNSETS, and TCL_GLOBAL_ONLY.  Not all flags are used by all
  46. procedures.  See below for more information.
  47. .AP Tcl_VarTraceProc *proc in
  48. Procedure to invoke whenever one of the traced operations occurs.
  49. .AP ClientData clientData in
  50. Arbitrary one-word value to pass to \fIproc\fR.
  51. .AP char *name1 in
  52. Name of scalar or array variable (without array index).
  53. .AP char *name2 in
  54. For a trace on an element of an array, gives the index of the
  55. element.  For traces on scalar variables or on whole arrays,
  56. is NULL.
  57. .AP ClientData prevClientData in
  58. If non-NULL, gives last value returned by \fBTcl_VarTraceInfo\fR or
  59. \fBTcl_VarTraceInfo2\fR, so this call will return information about
  60. next trace.  If NULL, this call will return information about first
  61. trace.
  62. .BE
  63.  
  64. .SH DESCRIPTION
  65. .PP
  66. \fBTcl_TraceVar\fR allows a C procedure to monitor and control
  67. access to a Tcl variable, so that the C procedure is invoked
  68. whenever the variable is read or written or unset.
  69. If the trace is created successfully then \fBTcl_TraceVar\fR returns
  70. TCL_OK.  If an error occurred (e.g. \fIvarName\fR specifies an element
  71. of an array, but the actual variable isn't an array) then TCL_ERROR
  72. is returned and an error message is left in \fIinterp->result\fR.
  73. .PP
  74. The \fIflags\fR argument to \fBTcl_TraceVar\fR indicates when the
  75. trace procedure is to be invoked and provides information
  76. for setting up the trace.  It consists of an OR-ed combination
  77. of any of the following values:
  78. .TP
  79. \fBTCL_GLOBAL_ONLY\fR
  80. Normally, the variable will be looked up at the current level of
  81. procedure call;  if this bit is set then the variable will be looked
  82. up at global level, ignoring any active procedures.
  83. .TP
  84. \fBTCL_TRACE_READS\fR
  85. Invoke \fIproc\fR whenever an attempt is made to read the variable.
  86. .TP
  87. \fBTCL_TRACE_WRITES\fR
  88. Invoke \fIproc\fR whenever an attempt is made to modify the variable.
  89. .TP
  90. \fBTCL_TRACE_UNSETS\fR
  91. Invoke \fIproc\fR whenever the variable is unset.
  92. A variable may be unset either explicitly by an \fBunset\fR command,
  93. or implicitly when a procedure returns (its local variables are
  94. automatically unset) or when the interpreter is deleted (all
  95. variables are automatically unset).
  96. .PP
  97. Whenever one of the specified operations occurs on the variable,
  98. \fIproc\fR will be invoked.
  99. It should have arguments and result that match the type
  100. \fBTcl_VarTraceProc\fR:
  101. .nf
  102. .RS
  103. typedef char *Tcl_VarTraceProc(
  104. .RS
  105. ClientData \fIclientData\fR,
  106. Tcl_Interp *\fIinterp\fR,
  107. char *\fIname1\fR,
  108. char *\fIname2\fR,
  109. int \fIflags\fR);
  110. .RE
  111. .RE
  112. .fi
  113. The \fIclientData\fP and \fIinterp\fP parameters will
  114. have the same values as those passed to \fBTcl_TraceVar\fR when the
  115. trace was created.
  116. \fIClientData\fR typically points to an application-specific
  117. data structure that describes what to do when \fIproc\fR
  118. is invoked.
  119. \fIName1\fR and \fIname2\fR give the name of the traced variable
  120. in the normal two-part form (see the description of \fBTcl_TraceVar2\fR
  121. below for details).
  122. \fIFlags\fR is an OR-ed combination of bits providing several
  123. pieces of information.
  124. One of the bits TCL_TRACE_READS, TCL_TRACE_WRITES, or TCL_TRACE_UNSETS
  125. will be set in \fIflags\fR to indicate which operation is being performed
  126. on the variable.
  127. The bit TCL_GLOBAL_ONLY will be set whenever the variable being
  128. accessed is a global one not accessible from the current level of
  129. procedure call:  the trace procedure will need to pass this flag
  130. back to variable-related procedures like \fBTcl_GetVar\fR if it
  131. attempts to access the variable.
  132. The bit TCL_TRACE_DESTROYED will be set in \fIflags\fR if the trace is
  133. about to be destroyed;  this information may be useful to \fIproc\fR
  134. so that it can clean up its own internal data structures (see
  135. the section TCL_TRACE_DESTROYED below for more details).
  136. Lastly, the bit TCL_INTERP_DESTROYED will be set if the entire
  137. interpreter is being destroyed.
  138. When this bit is set, \fIproc\fR must be especially careful in
  139. the things it does (see the section TCL_INTERP_DESTROYED below).
  140. The trace procedure's return value should normally be NULL;  see
  141. ERROR RETURNS below for information on other possibilities.
  142. .PP
  143. \fBTcl_UntraceVar\fR may be used to remove a trace.
  144. If the variable specified by \fIinterp\fR, \fIvarName\fR, and \fIflags\fR
  145. has a trace set with \fIflags\fR, \fIproc\fR, and
  146. \fIclientData\fR, then the corresponding trace is removed.
  147. If no such trace exists, then the call to \fBTcl_UntraceVar\fR
  148. has no effect.
  149. The same bits are valid for \fIflags\fR as for calls to \fBTcl_TraceVars\fR.
  150. .PP
  151. \fBTcl_VarTraceInfo\fR may be used to retrieve information about
  152. traces set on a given variable.
  153. The return value from \fBTcl_VarTraceInfo\fR is the \fIclientData\fR
  154. associated with a particular trace.
  155. The trace must be on the variable specified by the \fIinterp\fR,
  156. \fIvarName\fR, and \fIflags\fR arguments (only the TCL_GLOBAL_ONLY
  157. bit from \fIflags\fR is used;  other bits are ignored) and its trace procedure
  158. must the same as the \fIproc\fR argument.
  159. If the \fIprevClientData\fR argument is NULL then the return
  160. value corresponds to the first (most recently created) matching
  161. trace, or NULL if there are no matching traces.
  162. If the \fIprevClientData\fR argument isn't NULL, then it should
  163. be the return value from a previous call to \fBTcl_VarTraceInfo\fR.
  164. In this case, the new return value will correspond to the next
  165. matching trace after the one whose \fIclientData\fR matches
  166. \fIprevClientData\fR, or NULL if no trace matches \fIprevClientData\fR
  167. or if there are no more matching traces after it.
  168. This mechanism makes it possible to step through all of the
  169. traces for a given variable that have the same \fIproc\fR.
  170.  
  171. .SH "TWO-PART NAMES"
  172. .PP
  173. The procedures \fBTcl_TraceVar2\fR, \fBTcl_UntraceVar2\fR, and
  174. \fBTcl_VarTraceInfo2\fR are identical to \fBTcl_TraceVar\fR,
  175. \fBTcl_UntraceVar\fR, and \fBTcl_VarTraceInfo\fR, respectively,
  176. except that the name of the variable has already been
  177. separated by the caller into two parts.
  178. \fIName1\fR gives the name of a scalar variable or array,
  179. and \fIname2\fR gives the name of an element within an
  180. array.
  181. If \fIname2\fR is NULL it means that either the variable is
  182. a scalar or the trace is to be set on the entire array rather
  183. than an individual element (see WHOLE-ARRAY TRACES below for
  184. more information).
  185.  
  186. .SH "ACCESSING VARIABLES DURING TRACES"
  187. .PP
  188. During read and write traces, the
  189. trace procedure can read or write the value of the traced
  190. variable using \fBTcl_GetVar2\fR, \fBTcl_SetVar2\fR, and
  191. other procedures.
  192. While \fIproc\fR is executing, traces are temporarily disabled
  193. for the variable, so that calls to \fBTcl_GetVar2\fR and
  194. \fBTcl_SetVar2\fR will not cause \fIproc\fR or other trace procedures
  195. to be invoked again.
  196. Disabling only occurs for the variable whose trace procedure
  197. is active;  accesses to other variables will still be traced.
  198. .PP
  199. During unset traces the variable has already been completely
  200. expunged.
  201. It is possible for the trace procedure to read or write the
  202. variable, but this will be a new version of the variable.
  203. Traces are not disabled during unset traces as they are for
  204. read and write traces, but existing traces have been removed
  205. from the variable before any trace procedures are invoked.
  206. If new traces are set by unset trace procedures, these traces
  207. will be invoked on accesses to the variable by the trace
  208. procedures.
  209.  
  210. .SH "CALLBACK TIMING"
  211. .PP
  212. When read tracing has been specified for a variable, the trace
  213. procedure will be invoked whenever the variable's value is
  214. read.  This includes \fBset\fR Tcl commands, \fB$\fR-notation
  215. in Tcl commands, and invocations of the \fBTcl_GetVar\fR
  216. and \fBTcl_GetVar2\fR procedures.
  217. \fIProc\fR is invoked just before the variable's value is
  218. returned.
  219. It may modify the value of the variable to affect what
  220. is returned by the traced access.
  221. .PP
  222. When write tracing has been specified for a variable, the
  223. trace procedure will be invoked whenever the variable's value
  224. is modified.  This includes \fBset\fR commands\fR,
  225. commands that modify variables as side effects (such as
  226. \fBcatch\fR and \fBscan\fR), and calls to the \fBTcl_SetVar\fR
  227. and \fBTcl_SetVar2\fR procedures).
  228. \fIProc\fR will be invoked after the variable's value has been
  229. modified, but before the new value of the variable has been
  230. returned.
  231. It may modify the value of the variable to override the change
  232. and to determine the value actually returned by the traced
  233. access.
  234. .PP
  235. When unset tracing has been specified, the trace procedure
  236. will be invoked whenever the variable is destroyed.
  237. The traces will be called after the variable has been
  238. completely unset.
  239.  
  240. .SH "WHOLE-ARRAY TRACES"
  241. .PP
  242. If a call to \fBTcl_TraceVar\fR or \fBTcl_TraceVar2\fR specifies
  243. the name of an array variable without an index into the array,
  244. then the trace will be set on the array as a whole.
  245. This means that \fIproc\fR will be invoked whenever any
  246. element of the array is accessed in the ways specified by
  247. \fIflags\fR.
  248. When an array is unset, a whole-array trace will be invoked
  249. just once, with \fIname1\fR equal to the name of the array
  250. and \fIname2\fR NULL;  it will not be invoked once for each
  251. element.
  252.  
  253. .SH "MULTIPLE TRACES"
  254. .PP
  255. It is possible for multiple traces to exist on the same variable.
  256. When this happens, all of the trace procedures will be invoked on each
  257. access, in order from most-recently-created to least-recently-created.
  258. When there exist whole-array traces for an array as well as
  259. traces on individual elements, the whole-array traces are invoked
  260. before the individual-element traces.
  261.  
  262. .SH "ERROR RETURNS"
  263. .PP
  264. Under normal conditions trace procedures should return NULL, indicating
  265. successful completion.
  266. If \fIproc\fR returns a non-NULL value it signifies that an
  267. error occurred.
  268. The return value must be a pointer to a static character string
  269. containing an error message.
  270. If a trace procedure returns an error, no further traces are
  271. invoked for the access and the traced access aborts with the
  272. given message.
  273. Trace procedures can use this facility to make variables
  274. read-only, for example (but note that the value of the variable
  275. will already have been modified before the trace procedure is
  276. called, so the trace procedure will have to restore the correct
  277. value).
  278. .PP
  279. The return value from \fIproc\fR is only used during read and
  280. write tracing.
  281. During unset traces, the return value is ignored and all relevant
  282. trace procedures will always be invoked.
  283.  
  284. .SH "RESTRICTIONS"
  285. .PP
  286. It is not legal to delete a variable while a trace procedure
  287. is active for the variable.
  288. .PP
  289. .VS
  290. Also, a trace procedure can be called at any time, even when there
  291. is a partically-formed result in the interpreter's result area.  If
  292. the trace procedure does anything that could damage this result (such
  293. as calling \fBTcl_Eval\fR) then it must save the original values of
  294. the interpreter's \fBresult\fR and \fBfreeProc\fR fields and restore
  295. them before it returns.
  296. .VE
  297.  
  298. .SH "UNDEFINED VARIABLES"
  299. .PP
  300. It is legal to set a trace on an undefined variable.
  301. The variable will still appear to be undefined until the
  302. first time its value is set.
  303. If an undefined variable is traced and then unset, the unset will fail
  304. with an error (``no such variable''), but the trace
  305. procedure will still be invoked.
  306.  
  307. .SH "TCL_TRACE_DELETED FLAG"
  308. .PP
  309. In an unset callback to \fIproc\fR, the TCL_TRACE_DELETED bit
  310. is set in \fIflags\fR if the trace is being removed as part
  311. of the deletion.
  312. Traces on a variable are always removed whenever the variable
  313. is deleted;  the only time TCL_TRACE_DELETED isn't set is for
  314. a whole-array trace invoked when only a single element of an
  315. array is unset.
  316.  
  317. .SH "TCL_INTERP_DESTROYED"
  318. .PP
  319. When an interpreter is destroyed, unset traces are called for
  320. all of its variables.
  321. The TCL_INTERP_DESTROYED bit will be set in the \fIflags\fR
  322. argument passed to the trace procedures.
  323. Trace procedures must be extremely careful in what they do if
  324. the TCL_INTERP_DESTROYED bit is set.
  325. It is not safe for the procedures to invoke any Tcl procedures
  326. on the interpreter, since its state is partially deleted.
  327. All that trace procedures should do under these circumstances is
  328. to clean up and free their own internal data structures.
  329.  
  330. .SH BUGS
  331. .PP
  332. Tcl doesn't do any error checking to prevent trace procedures
  333. from misusing the interpreter during traces with TCL_INTERP_DESTROYED
  334. set.
  335.  
  336. .SH KEYWORDS
  337. clientData, trace, variable
  338. .VE
  339.